﻿namespace EFS.RC.ConversationBrowser.Controls.Actions
{
	public partial class ViewExternal : EFS.RC.ConversationBrowser.Controls.Control
	{
		#region Constructors
		/// Implicit
		public ViewExternal()
		{
			InitializeComponent();

			// do not use this
			ConstructorNotAllowed();
		}

		/// Explicit
		public ViewExternal(
			System.Guid conversationGuid,
			System.DateTime conversationDate)
		{
			InitializeComponent();

			// add controls from code
			// menuItemSurvey
			System.Windows.Controls.MenuItem menuItemSurvey = new System.Windows.Controls.MenuItem();
			menuItemSurvey.Height = 22;
			menuItemSurvey.Header = "Monitoring";
			menuItemSurvey.Click += menuItemSurvey_Click;
			gridMenuItemSurvey.Children.Clear();
			gridMenuItemSurvey.Children.Add(menuItemSurvey);

			HideSurvey();

			// set call backs
			UtilitiesService.EventLogs_GetConversationEventsCompleted += new System.EventHandler<Proxy.EventLogs_GetConversationEventsCompletedEventArgs>(UtilitiesService_EventLogs_GetConversationEventsCompleted);

			// set
			this.ConversationGuid = conversationGuid;
			this.ConversationDate = conversationDate;

			controlPlay.ConversationGuid = conversationGuid;
			controlPlay.ConversationDate = conversationDate;
		}
		#endregion

		#region Properties
		/// Property ConversationGuid: Get, Set
		public System.Guid ConversationGuid { get; private set;}

		/// Property ConversationDate: Get, Set
		public System.DateTime ConversationDate { get; private set;}
		#endregion

		#region LoadData
		/// LoadData
		private void LoadData()
		{
			// show wait
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Loading Conversation Events");

			// get data
			UtilitiesService.EventLogs_GetConversationEventsAsync(ConversationGuid);
		}
		#endregion

		#region Utilities
		/// HideSurvey
		private void HideSurvey()
		{
			dockPanelSurvey.Children.Clear();
			scrollViewerSurvey.Visibility = System.Windows.Visibility.Collapsed;
			labelScoring.Text = "";
		}

		/// <summary>
		/// Show averagce chages
		/// </summary>
		/// <param name="s"></param>
		private void OnAverageChange(System.Double d) 
		{
			labelScoring.Text = "Scoring :" + d.ToString("##.##");
		}

		/// <summary>
		/// Open survey in place 
		/// </summary>
		/// <param name="surver"></param>
		private void OpenSurvey(EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo surveySessionObj)
		{
			// hide previous
			HideSurvey();

			// set scroll to beginning
			scrollViewerSurvey.ScrollToVerticalOffset(0);

			EFS.RC.Survey.Controls.Survey controlSurvey = null;
			if (surveySessionObj.Session.ID == null)
			{
				controlSurvey = new EFS.RC.Survey.Controls.Survey(surveySessionObj.Survey, ConversationGuid);
			}
			else
			{
				// create new
				controlSurvey = new EFS.RC.Survey.Controls.Survey(surveySessionObj);
			}
			controlSurvey.OnAverageChange += OnAverageChange;

			controlSurvey.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			controlSurvey.VerticalAlignment = System.Windows.VerticalAlignment.Top;
			controlSurvey.OnSaveCompleted = SurveyCompleted;
			controlSurvey.OnCancel = SurveyCanceled;
			dockPanelSurvey.Children.Add(controlSurvey);
			scrollViewerSurvey.Visibility = System.Windows.Visibility.Visible;
		}

		// SurveyCompleted
		private void SurveyCompleted(System.Int32 surveySessionId)
		{
			// hide survey
			HideSurvey();

			// show message
			System.Windows.MessageBox.Show("Your monitoring has been saved.", "Information", System.Windows.MessageBoxButton.OK);
		}

		// SurveyCompleted
		private void SurveyCanceled()
		{
			// hide survey
			HideSurvey();
		}

		/// Jump to selected position in player
		private void JumpTo()
		{
			// get selected item
			EFS.RC.ConversationBrowser.Tools.Conversation.Event evtObj = (EFS.RC.ConversationBrowser.Tools.Conversation.Event)dataGridResults.SelectedItem;
			if (null == evtObj || !evtObj.OccurredFromBeginningTS.HasValue)
			{
				return;
			}

			// play/seek
			controlPlay.controlPlayer.Play();
			controlPlay.controlPlayer.SeekToPosition(evtObj.OccurredFromBeginningTS.Value);
		}
		#endregion

		#region WS Callbacks
		/// Conversation_SearchCompleted
		void UtilitiesService_EventLogs_GetConversationEventsCompleted(object sender, Proxy.EventLogs_GetConversationEventsCompletedEventArgs e)
		{
			// close wait
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.ConversationBrowser.Tools.Conversation.EventList resultList =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.ConversationBrowser.Tools.Conversation.EventList>(e.Result.ToString());

				// set to grid
				dataGridResults.ItemsSource = resultList.Items;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve events.", ex);
			}
		}
		#endregion

		#region Events
		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			LoadData();
		}

		/// Jump to in conversation
		private void hyperlinkButtonJumpTo_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			JumpTo();
		}

		/// dataGridHighwayResults_DoubleClick
		private void dataGridResults_DoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			JumpTo();
		}

		/// Survey click
		private void menuItemSurvey_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// create new
			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.ButtonOKVisible = false;
			EFS.RC.Survey.Controls.List surveyList = new EFS.RC.Survey.Controls.List(wnd, ConversationGuid, null);
			surveyList.OnOpenSurvey = OpenSurvey;
			wnd.HostedContent = surveyList;
			wnd.Show(null);
		}
		#endregion
	}

}